package com.hopes.bizz.poll.town.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.om.bo.OmData;
import com.hopes.bizz.poll.town.dto.PollTownDataAlterDto;
import com.hopes.bizz.poll.town.dto.TownHisDataDTO;
import com.hopes.bizz.poll.town.dto.TownHisTrendDTO;
import com.hopes.bizz.poll.town.entity.BaseTownOmDataEntity;
import com.hopes.bizz.poll.town.vo.PollTownAnalysisDataVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ljh
 * @create: 2023/10/10
 * 乡镇历史数据相关的查询*
 */
public interface TownHisService {

    Page<BaseTownOmDataEntity> queryTownHisData(Page page, TownHisDataDTO townHisDataDTO);

    // 数据趋势分析
    PollTownAnalysisDataVO trendAnalysis(TownHisTrendDTO townHisTrendDTO);

    /**
     * @Description 修约数据
     **/
    void alter(PollTownDataAlterDto pollTownDataAlterDto);

    // 构建实时数据非折算值 trendAnalysis使用的方法
    default List<Double> buildData(List<? extends BaseTownOmDataEntity> hisData, String factorCode) {
        ArrayList<Double> list = new ArrayList<>();
        for (BaseTownOmDataEntity hisDatum : hisData) {
            JSONObject map = hisDatum.getDataJson();
            JSONObject code = map.getJSONObject(factorCode);
            if (ObjectUtil.isNotEmpty(code)) {
                OmData data = JSONObject.toJavaObject(code, OmData.class);
                BigDecimal val = data.getVal();
                list.add(val.doubleValue());
            }
        }
        return list;
    }
}
